/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.basictree.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.basicdata.basictree.entity.PartDeviceNode;
import com.jeeplus.modules.basicdata.basictree.entity.PartDeviceNodeCode;
import com.jeeplus.modules.basicdata.basictree.mapper.PartDeviceNodeMapper;
import com.jeeplus.modules.basicdata.basictree.service.PartDeviceNodeService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备节点Controller
 * @author 李宏博
 * @version 2020-09-28
 */
@RestController
@RequestMapping(value = "/basicdata/basictree/partDeviceNode")
public class PartDeviceNodeController extends BaseController {

	@Autowired
	private PartDeviceNodeService partDeviceNodeService;

	@ModelAttribute
	public PartDeviceNode get(@RequestParam(required=false) String id) {
		PartDeviceNode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partDeviceNodeService.get(id);
		}
		if (entity == null){
			entity = new PartDeviceNode();
		}
		return entity;
	}


	/**
	 * 设备节点树表数据
	 */
	@RequiresPermissions("basicdata:basictree:partDeviceNode:list")
	@GetMapping("list")
	public AjaxJson list(PartDeviceNode partDeviceNode) {
		return AjaxJson.success().put("list", partDeviceNodeService.findList(partDeviceNode));
	}

	/**
	 * 根据Id获取设备节点数据
	 */
	@RequiresPermissions(value={"basicdata:basictree:partDeviceNode:view","basicdata:basictree:partDeviceNode:add","basicdata:basictree:partDeviceNode:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(PartDeviceNode partDeviceNode) {
		return AjaxJson.success().put("partDeviceNode", partDeviceNode);
	}
	/**
	 * 根据code判断是否有重复数据
	 * 传入为code返回为partDeviceNodeCode 如果存在code返回BooleanCode为true和名称 不存在则返回BooleanCode为false
	 */
	@RequiresPermissions(value={"basicdata:basictree:partDeviceNode:add","basicdata:basictree:partDeviceNode:edit"},logical=Logical.OR)
	@GetMapping("queryCode")
	public AjaxJson queryCode(String code) {
		String name = partDeviceNodeService.codeGet(code);
		boolean booleanCode ;
		if(name == null){
			booleanCode = false;
		}else{
			booleanCode = true;
		}
		PartDeviceNodeCode partDeviceNodeCode = new PartDeviceNodeCode();
		partDeviceNodeCode.setName(name);
		partDeviceNodeCode.setBooleanCode(booleanCode);
		return AjaxJson.success().put("PartDeviceNodeCode", partDeviceNodeCode);
	}

	@RequiresPermissions(value={"basicdata:basictree:partDeviceNode:add","basicdata:basictree:partDeviceNode:edit"},logical=Logical.OR)
	@GetMapping("queryCodeReturn")
	public AjaxJson queryCodeReturn(String code , String level) {
		String codeName = partDeviceNodeService.codeReturn(code, level);
		return AjaxJson.success().put("code", codeName);
	}


	/**
	 * 保存设备节点
	 */
	@RequiresPermissions(value={"basicdata:basictree:partDeviceNode:add","basicdata:basictree:partDeviceNode:addLevelOne","basicdata:basictree:partDeviceNode:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(PartDeviceNode partDeviceNode, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(partDeviceNode);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		partDeviceNodeService.save(partDeviceNode);//保存
		return AjaxJson.success("保存设备节点成功");
	}

	/**
	 * 删除设备节点
	 */
	@RequiresPermissions("basicdata:basictree:partDeviceNode:del")
	@DeleteMapping("delete")
	public AjaxJson delete(PartDeviceNode partDeviceNode) {
		partDeviceNodeService.delete(partDeviceNode);
		return AjaxJson.success("删除设备节点成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<PartDeviceNode> list = partDeviceNodeService.findList(new PartDeviceNode());
		List rootTree = getPartDeviceNodeTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<PartDeviceNode> getPartDeviceNodeTree(List<PartDeviceNode> list, String extId) {
		List<PartDeviceNode> partDeviceNodes = Lists.newArrayList();
		List<PartDeviceNode> rootTrees = partDeviceNodeService.getChildren("0");
		for (PartDeviceNode root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        partDeviceNodes.add(getChildOfTree(root, list, extId));
		    }
		}
		return partDeviceNodes;
	}

	private PartDeviceNode getChildOfTree(PartDeviceNode partDeviceNode, List<PartDeviceNode> partDeviceNodeList, String extId) {
		partDeviceNode.setChildren(Lists.newArrayList());
		for (PartDeviceNode child : partDeviceNodeList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(partDeviceNode.getId())) {
		            partDeviceNode.getChildren().add(getChildOfTree(child, partDeviceNodeList, extId));
		        }
		    }
		}
		return partDeviceNode;
	}

	/**
	 * 获取当前节点的所有下一级子节点
	 *
	 * @param id 父节点的id值
	 * @param extId
	 * @return
	 */
	@RequiresPermissions("user")
	@GetMapping("nextLevelTreeData")
	public AjaxJson nextLevelTreeData(@RequestParam(required = false)String id, @RequestParam(required = false) String extId ,PartDeviceNode partDeviceNode) {
		List<PartDeviceNode> nextLevelTreeData = null;
		if (StringUtils.isBlank(id)) {
			nextLevelTreeData = partDeviceNodeService.getChildrenDataruler("0",partDeviceNode);
		} else {
			nextLevelTreeData = partDeviceNodeService.getChildren(id);
		}
		return AjaxJson.success().put("data", nextLevelTreeData);
	}
}